package com.cisco.pso;

import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.io.FileUtils;

public class Merger {
	public static void main(String[] args) throws IOException {
		// silence means error will be output as comment in shell script.
		boolean silence = true;

		boolean checkSubFolder = false;

		String site = "sso";
		String siteAbbreviation = "Psosso";
		// String module = "webcomponents";

		String baseLeftPath = "base/T27L10NSP32/";
		String baseRightPath = "base/T28L10NSP12/";

		String psoLeftPath = "pso/T27/";
		String psoRightPath = "pso/T28/";

		String baseDiffFile = "base_diff_file_list";
		// String psoDiffFile = "pso_chemical_" + module + "_diff";
		String psoFileList = "pso_" + site + "_file_list";

		InputStreamReader baseInput = new InputStreamReader(Merger.class
				.getClassLoader().getResourceAsStream(baseDiffFile));
		InputStreamReader psoInput = new InputStreamReader(Merger.class
				.getClassLoader().getResourceAsStream(psoFileList));

		DiffReader baseDr = new DiffReader(baseInput, baseLeftPath,
				baseRightPath);
		PsoFileListReader psoLR = new PsoFileListReader(psoInput,
				siteAbbreviation);

		System.out.println("#!/bin/bash");
		System.out.println("");

		int matchProblemCount = 0;
		for (FileItem fi : psoLR.getItems()) {
			PsoFileItem psoFile = (PsoFileItem) fi;

			DiffResult baseResult = null;

			List<DiffResult> matchDiffResults = new ArrayList<DiffResult>();
			for (DiffResult dr : baseDr.getResults()) {
				String baseFileName = null;
				String psoFileName = null;

				if (checkSubFolder) {
					baseFileName = dr.getSubFolder(psoFile.getSubFolderDepth())
							+ dr.getShortFileName();
					psoFileName = psoFile.getSubFolder()
							+ psoFile.getBaseName();
				} else {
					baseFileName = dr.getShortFileName();
					psoFileName = psoFile.getBaseName();
				}

				// strict compare with module, but the pso-shared module is for
				// base waf module
				String baseModule = dr.getModule();
				String psoModule = psoFile.getModule();

				boolean isModuleMatch = baseModule.equalsIgnoreCase(psoModule);
				// if(baseModule.equals("waf") && psoModule.equals("shared"))
				// isModuleMatch=true;

				//System.out.println(baseFileName + "?" + psoFileName);
//				if(baseFileName.equalsIgnoreCase("MeetingInfoAction.java"))
//				{
//					System.out.println(baseModule+"-"+baseFileName + "?" + psoModule +"-"+ psoFileName);
//				}
				
				if (isModuleMatch && baseFileName.equalsIgnoreCase(psoFileName)) {
					matchDiffResults.add(dr);
					baseResult = dr;
				}
			}

			if (matchDiffResults.size() > 1) {
				matchProblemCount++;
				if (silence) {
					System.out.println("#error: non-unique match:"
							+ psoFile.getFullName());
				} else {
					StringBuilder sb = new StringBuilder();
					sb.append("non-unique match!\n");
					sb.append("pso file:" + psoFile.getFullName() + "\n");
					sb.append("base files:\n");
					for (DiffResult dr : matchDiffResults) {
						sb.append(dr.getRaw() + "\n");
					}
					throw new RuntimeException(sb.toString());
				}
			}

			if (matchDiffResults.size() == 1) {
				StringBuilder sb = new StringBuilder();
				sb.append("diff3 -m --strip-trailing-cr ");
				sb.append(psoFile.getFullName() + " ");
				sb.append(baseResult.getLeftFile() + " ");
				sb.append(baseResult.getRightFile() + " > ");

				String psoRightFile = psoFile.getFullName().replaceFirst(
						psoLeftPath, psoRightPath);
				sb.append(psoRightFile);

				System.out.println(sb.toString());
			}

		}

		if (silence)
			System.out.println("#total match problems:" + matchProblemCount);
	}
}
